/**
 * 
 */
package habitcoach.core.habit;

import static org.junit.Assert.assertEquals;
import habitcoach.core.task.Task;
import habitcoach.core.task.TaskImpl;

import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import test.mock.MockEnvironment;
import util.Constants;

/**
 * @author Coddy created on 2010-4-25
 * 
 */
public class HabitImplTest
{

	HabitImpl habit;

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Before
	public void setUp() throws Exception
	{
		habit = (HabitImpl) HabitFactory.getInstance().createHabit();
	}

	/**
	 * Test method for
	 * {@link habitcoach.core.habit.HabitImpl#appendTask(habitcoach.core.task.Task)}
	 * .
	 */
	@Test
	public void testAppendTask()
	{
		int i = habit.getTaskCount();
		habit.appendTask(new TaskImpl());

		assertEquals(i + 1, habit.getTaskCount());
	}

	/**
	 * Test method for {@link habitcoach.core.habit.HabitImpl#getQuestion()}.
	 */
	@Test
	public void testGetQuestion()
	{
		assertEquals(Constants.DEFAULT_DAILY_QUESTION, habit.getQuestion());

		String question = "Test";
		Habit h = HabitFactory.getInstance().createHabit(question);
		assertEquals(question, h.getQuestion());
	}

	/**
	 * Test method for {@link habitcoach.core.habit.HabitImpl#getStartDate()}.
	 */
	@Test
	public void testGetStartDate()
	{
		MockEnvironment mockEnvironment = new MockEnvironment();
		HabitFactory factory = HabitFactory.getInstance(mockEnvironment);

		Habit habit = factory.createHabit();
		assertEquals(mockEnvironment.getDate(), habit.getStartDate());
	}

	/**
	 * Test method for {@link habitcoach.core.habit.HabitImpl#getStatus()}.
	 */
	@Test
	public void testGetStatus()
	{
		assertEquals(HabitStatus.UNSTARTED, habit.getStatus());
		habit.setStatus(HabitStatus.FINISHED);
		assertEquals(HabitStatus.FINISHED, habit.getStatus());
	}

	/**
	 * Test method for {@link habitcoach.core.habit.HabitImpl#onDateChanged()}.
	 */
	@Test
	public void testOnDayChanged()
	{
		int currentIndex = habit.getIndex();
		habit.onDateChanged();
		assertEquals(currentIndex + 1, habit.getIndex());
	}

	@Test
	public void testGetTaskIndex()
	{
		List<Task> list = habit.getTaskList();
		for (int i = 0; i < list.size(); i++)
		{
			assertEquals(list.get(i), habit.getTask(i));
		}

	}

	@Test(expected = IndexOutOfBoundsException.class)
	public void testGetTaskIndexInvalid()
	{
		List<Task> list = habit.getTaskList();

		habit.getTask(list.size());
		// assertNull(habit.getTask(list.size()));
	}

	@Test
	public void testGetID()
	{
		int counter = HabitFactory.getInstance().createHabit().getID();

		habit = (HabitImpl) HabitFactory.getInstance().createHabit();

		Assert.assertEquals(counter + 1, habit.getID());
		habit.setID(5);
		Assert.assertEquals(5, habit.getID());

	}
}
